using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SchemaMonitor;
//using Framework;

public partial class pages_Chapters_usercontrols_UCChapters : UserControl
{    
    #region Constants
    public const string REMAINING = "Remaining";
    public const string SELECTED  = "Selected";
	#endregion


	#region Members
	private CChapterList _list;
	#endregion

	#region Interface for Nodes page
	//Remaining Sections
	public void DisplayRemaining(CNode node, string search)
    {
        litSelected.Text = REMAINING;
        litCount.Text = CSection.Cache.Count.ToString();
        colDate.Visible = false;

        CSectionList filtered = node.Chapters.RemainingSections(search);
        foreach (CSection i in ctrlPaging.Display(filtered))
            UCChapter(plh).Display(i, node.NodeGuid, null, filtered.IndexOf(i) + 1);
    }
    //Selected Sections
    public void DisplaySelected(CNode node)
    {
        litSelected.Text = SELECTED;
        CChapterList selected = node.Chapters;
        litCount.Text = selected.Count.ToString();
        foreach (CChapter i in ctrlPaging.Display(selected))
            UCChapter(plh).Display(i.Section, i.ChapterNodeGuid, i, selected.IndexOf(i) + 1);
    }
    #endregion
    
    #region Interface for Sections page
    //Remaining Nodes
    public void DisplayRemaining(CSection section, string search, CUser u, bool forBook)
    {

        litSelected.Text = REMAINING;
        litCount.Text =  section.Posts(u).Count.ToString();

		colDate.Visible = true;
		colOrder.Visible = false;
		colRating.Visible = false;

		CNodeList all = section.RemainingNodes(u, search, forBook);
		var page = ctrlPaging.Display(all);
		var pi = PagingInfo;
		foreach (CNode i in page)
        {
            int index = page.IndexOf(i) + 1 + pi.Offset;
            UCChapter(plh).Display(i, section.SectionId, null, index);
        }

	}
    //Selected Nodes
    public void DisplaySelected(CSection section, CUser u)
	{
		_list = section.Chapters;

		colDate.Visible = true;
		colOrder.Visible = (section.Chapters.Count > 1);

		litSelected.Text = SELECTED;
        litCount.Text = section.Chapters.Count.ToString();
		var page = ctrlPaging.Display(section.Chapters);
        foreach (CChapter i in page)
        {
            int number = page.IndexOf(i) + 1 + PagingInfo.Offset;
            UCChapter(plh).Display(i.Node_(u), i.ChapterSectionId, i, number);
        }
    }
    #endregion
    
    #region User Controls
    private pages_Chapters_usercontrols_UCChapter UCChapter(Control target)
    {
        Control ctrl = Page.LoadControl(CSitemap.UCChapter());
        target.Controls.Add(ctrl);
        return (pages_Chapters_usercontrols_UCChapter)ctrl;
    }
    #endregion
    
    #region Appearance
    public string Title 
    {
        get { return litTitle.Text; }
        set { litTitle.Text = value; }
    }
	#endregion

	#region Event Handlers

	public void btnResetOrderToAlpha_Click(object sender, EventArgs e)
	{
		_list.SortAlphabetically(); //Non-cached classes require a custom update query (or apply to unpaged data)
		Response.Redirect(Request.RawUrl);
	}
	#endregion

	#region Paging
	public Framework.CPagingInfo PagingInfo { get { return ctrlPaging.Info; } }
    public string QueryString 
    {
        get { return ctrlPaging.QueryString; }
        set { ctrlPaging.QueryString = value; }
    }
    public int PageSize 
    {
        get { return ctrlPaging.PageSize; }
        set { ctrlPaging.PageSize = value; }
    }
    public string FriendlyUrl 
    {
        set { ctrlPaging.FriendlyUrl = string.Concat(value, (value.Contains("?") ? "&" : "?"), QueryString, "={0}"); }
    }
    #endregion
    
}